home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / space / software / unix / xanim / xanm_tls.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-04  |  6.3 KB  |  288 lines

  1.  
  2. /*
  3.  * xanim_utils.c
  4.  *
  5.  * Copyright (C) 1991,1992 by Mark Podlipec. 
  6.  * All rights reserved.
  7.  *
  8.  * This software may be freely copied, modified and redistributed
  9.  * without fee provided that this copyright notice is preserved 
  10.  * intact on all copies and modified copies.
  11.  * 
  12.  * There is no warranty or other guarantee of fitness of this software.
  13.  * It is provided solely "as is". The author(s) disclaim(s) all
  14.  * responsibility and liability with respect to this software's usage
  15.  * or its effect upon hardware or computer systems.
  16.  *
  17.  */
  18. #include "xanim.h"
  19. #include "xanim_utils.h"
  20.  
  21. void UTIL_Sub_Image();
  22. void UTIL_Create_Clip();
  23. void UTIL_Sub_Mapped_To_True();
  24. void UTIL_Mapped_To_Bitmap();
  25. void UTIL_Half_Image_Height();
  26. void UTIL_Pack_Image();
  27.  
  28.  
  29. void
  30. UTIL_Half_Image_Height(out,in,xpos,ypos,xsize,ysize,line_size)
  31. UBYTE *out,*in;
  32. ULONG xpos,*ypos,xsize,*ysize,line_size;
  33. {
  34.   ULONG t_ysize, t_ypos, y_start, j;
  35.   register ULONG y,start;
  36.   register UBYTE *src;
  37.  
  38.   t_ypos  = (*ypos);
  39.   t_ysize = (*ysize);
  40.  
  41.   if (t_ypos & 0x01) y_start = 1;    /* start on 2nd in line */
  42.   else y_start = 0;            /* start on 1st in line */
  43.  
  44.   start = (ULONG)in;
  45.   j=0;
  46.   for(y = y_start; y < t_ysize; y+=2)
  47.   {
  48.     src = (UBYTE *)(start + y * line_size);
  49.     memcpy( (char *)out, (char *)src, line_size);
  50.     out += line_size;
  51.     j++;
  52.   }
  53.   *ypos  = (t_ypos + 1)  >> 1;
  54.   *ysize = j;
  55. }
  56.  
  57. void
  58. UTIL_Sub_Image(out,in,xosize,yosize,xipos,yipos,xisize,pix_size)
  59. UBYTE *out,*in;
  60. ULONG xosize,yosize,xipos,yipos,xisize,pix_size;
  61. {
  62.  ULONG y,x;
  63.  UBYTE *in_ptr;
  64.  xosize *= pix_size;
  65.  xisize *= pix_size;
  66.  xipos  *= pix_size;
  67.  for(y=yipos; y < (yipos + yosize); y++)
  68.  {
  69.   in_ptr = (UBYTE *)( (ULONG)(in) + y * xisize + xipos);
  70.   x = xosize;
  71.   while(x--) *out++ = *in_ptr++;
  72.  } 
  73. }
  74.  
  75. void
  76. UTIL_Sub_Mapped_To_True(out,in,map,xosize,yosize,xipos,yipos,xisize)
  77. UBYTE *out,*in;
  78. ColorReg *map;
  79. ULONG xosize,yosize,xipos,yipos,xisize;
  80. {
  81.   ULONG y,x;
  82.   UBYTE *in_ptr;
  83.   for(y=yipos; y < (yipos + yosize); y++)
  84.   {
  85.     in_ptr = (UBYTE *)( (ULONG)(in) + y * xisize + xipos);
  86.     x = xosize;
  87.     if (x11_bytes_pixel == 4)
  88.     { 
  89.       ULONG *ulp = (ULONG *)out;
  90.       while(x--) *ulp++ = (ULONG)(map[*in_ptr++].map);
  91.       out = (UBYTE *)ulp;
  92.     }
  93.     else if (x11_bytes_pixel == 2)
  94.     {
  95.       USHORT *usp = (USHORT *)out;
  96.       while(x--) *usp++ = (USHORT)(map[*in_ptr++].map);
  97.       out = (UBYTE *)usp;
  98.     }
  99.     else while(x--) *out++ = (UBYTE)(map[*in_ptr++].map);
  100.  } 
  101. }
  102.  
  103. void
  104. UTIL_Create_Clip(out,in,pix_mask,xsize,ysize,pix_size)
  105. UBYTE *out,*in;
  106. ULONG pix_mask,xsize,ysize,pix_size;
  107. {
  108.   register ULONG data_in,bit_mask,x,y,mask_start,mask_end;
  109.   register UBYTE data_out;
  110.  
  111. /* NOTE: should probably look at bit order or are CLIPs always LSB */
  112.  
  113.   mask_start = 0x01;
  114.   mask_end = 0x80;
  115.  
  116.   for(y=0; y<ysize; y++)
  117.   {
  118.     bit_mask = mask_start;
  119.     data_out = 0;
  120.     x = xsize;
  121.     while(x--)
  122.     {
  123.       if (pix_size == 4) 
  124.     { ULONG *ulp = (ULONG *)in;    data_in = *ulp; }
  125.       else if (pix_size == 2)
  126.     { USHORT *usp = (USHORT *)in;    data_in = (ULONG)(*usp); }
  127.       else                data_in = (ULONG)(*in);
  128.       in += pix_size;
  129.  
  130.       if (data_in != pix_mask) data_out |= bit_mask;
  131.  
  132.       if (bit_mask == mask_end)
  133.       {
  134.         *out++ = data_out;
  135.         bit_mask = mask_start;
  136.     data_out = 0;
  137.       }
  138.       else if (mask_start == 0x01) bit_mask <<= 1;
  139.            else bit_mask >>= 1;
  140.     } /* end of while x */
  141.     if (bit_mask != mask_start) *out++ = data_out;
  142.   } /* end of for y */
  143. }
  144.  
  145. void
  146. UTIL_Pack_Image(out,in,xsize,ysize,pk_size)
  147. UBYTE *out,*in;
  148. ULONG xsize,ysize,pk_size;
  149. {
  150.  int i;
  151. }
  152.  
  153.  
  154. void
  155. UTIL_Mapped_To_Bitmap(out,in,map,csize,xsize,ysize,line_size)
  156. UBYTE *out,*in;
  157. ColorReg *map;
  158. ULONG csize, xsize, ysize, line_size;
  159. {
  160.   ULONG y,flag;
  161.   register ULONG mask,invert,x,mask_start,mask_end;
  162.   SHORT *imap,*c_ptr,*n_ptr,*t_ptr,*err_buff,err,threshold;
  163.   UBYTE data,*o_ptr;
  164.  
  165.   err_buff = (SHORT *)malloc(xsize * 2 * sizeof(short));
  166.   imap     = (SHORT *)malloc(csize * sizeof(short));
  167.  
  168.   for(x=0; x<csize; x++)
  169.     imap[x] = X11_Get_Monochrome(map[x].red,map[x].green,map[x].blue);
  170.  
  171.   for(x = 0; x < xsize; x++) err_buff[x] = (USHORT)imap[ *in++ ];
  172.   flag = 0;
  173.  
  174.   /* used to invert image */
  175.   if (x11_white & 0x01) invert = 0x00;
  176.   else invert = ~0x00;
  177.   threshold = 128;
  178.  
  179.   if (x11_bit_order == X11_MSB) { mask_start = 0x80; mask_end = 0x01; }
  180.   else { mask_start = 0x01; mask_end = 0x80; }
  181.  
  182.   for(y = 0; y < ysize; y++)
  183.   {
  184.     o_ptr = (UBYTE *)( (ULONG)(out) + line_size * y);
  185.     /* double buffer error arrays */
  186.     if (flag == 0) 
  187.     {
  188.       c_ptr = err_buff;
  189.       n_ptr = (SHORT *)( (ULONG)(err_buff) + xsize * sizeof(short) );
  190.       flag = 1;
  191.     }
  192.     else 
  193.     {
  194.       n_ptr = err_buff;
  195.       c_ptr = (SHORT *)( (ULONG)(err_buff) + xsize * sizeof(short) );
  196.       flag = 0;
  197.     }
  198.  
  199.     data = 0x00;
  200.     mask = mask_start;
  201.     if (y < (ysize - 1) )  n_ptr[0] = (USHORT)imap[ *in++ ];
  202.  
  203.     for(x=0; x<xsize; x++)
  204.     {
  205.       if (*c_ptr >= threshold) { err = *c_ptr - 255; data |= mask; } 
  206.       else  err = *c_ptr;
  207.  
  208.       if (mask == mask_end) 
  209.     { *o_ptr++ = data^invert; data = 0x00; mask = mask_start; }
  210.       else if (mask_start == 0x80) mask >>= 1;
  211.            else mask <<= 1;
  212.       c_ptr++;
  213.  
  214.       if (dither_flag == FALSE)
  215.       {
  216.         if (x < (xsize - 1) ) *n_ptr++ = imap[ *in++ ];
  217.       }
  218.       else
  219.       {
  220.         if (x < (xsize - 1) )  *c_ptr += (7 * err)/16;
  221.         if (y < (ysize - 1) )
  222.         {
  223.           if (x > 0) *n_ptr++ += (3 * err)/16;
  224.           *n_ptr++ += (5 * err)/16;
  225.           if (x < (xsize - 1) ) 
  226.           { *n_ptr = (USHORT)(imap[*in++]) + (SHORT)(err/16); n_ptr--; }
  227.         }
  228.       }
  229.     }
  230.     if (mask != mask_start) *o_ptr++ = data^invert; /* send out partial */
  231.  
  232.   }
  233.   free(err_buff);
  234.   free(imap);
  235. }
  236.  
  237. /* Routine to read a little endian long word.
  238.  */
  239. ULONG UTIL_Get_LSB_Long(fp)
  240. FILE *fp;
  241. {
  242.  ULONG ret;
  243.  
  244.  ret =  fgetc(fp);
  245.  ret |= fgetc(fp) << 8;
  246.  ret |= fgetc(fp) << 16;
  247.  ret |= fgetc(fp) << 24;
  248.  return ret;
  249. }
  250.  
  251. /* Routine to read a little endian half word.
  252.  */
  253. ULONG UTIL_Get_LSB_Short(fp)
  254. FILE *fp;
  255. {
  256.  ULONG ret;
  257.  
  258.  ret =  fgetc(fp);
  259.  ret |= fgetc(fp) << 8;
  260.  return ret;
  261. }
  262.  
  263. /* Routine to read a big endian long word.
  264.  */
  265. ULONG UTIL_Get_MSB_Long(fp)
  266. FILE *fp;
  267. {
  268.  ULONG ret;
  269.  
  270.  ret  = fgetc(fp) << 24;
  271.  ret |= fgetc(fp) << 16;
  272.  ret |= fgetc(fp) << 8;
  273.  ret |=  fgetc(fp);
  274.  return ret;
  275. }
  276.  
  277. /* Routine to read a big endian half word.
  278.  */
  279. ULONG UTIL_Get_MSB_Short(fp)
  280. FILE *fp;
  281. {
  282.  ULONG ret;
  283.  
  284.  ret  =  fgetc(fp) << 8;
  285.  ret |=  fgetc(fp);
  286.  return ret;
  287. }
  288.